package com.bjyhl.medical.main.video;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjyhl.ui.recycler.DataConverter;
import com.bjyhl.ui.recycler.ItemType;
import com.bjyhl.ui.recycler.MultipleFields;
import com.bjyhl.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * 新闻列表
 */
public class VideoListDataConverter extends DataConverter {
    @Override
    public ArrayList<MultipleItemEntity> convert() {
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");

        final int size = dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);

            final String title = data.getString("title");
            String title10 = title;
            if(title10.length() > 10) title10 = title10.substring(0, 10) + "...";
            final String content = data.getString("m3u8_name");
            final String thumb = data.getString("image");
            final Boolean is_cat = data.getInteger("is_cat") == 1;
            final Integer id = data.getInteger("id");

            int type = ItemType.VIDEO;

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.TITLE, title10)
                    .setField(MultipleFields.CONTENT, content)
                    .setField(MultipleFields.IMAGE_URL, thumb)
                    .setField(MultipleFields.TEXT, title)
                    .setField(MultipleFields.TAG, is_cat)
                    .setField(MultipleFields.ID, id)
                    .build();
            ENTITIES.add(entity);
        }

        return ENTITIES;
    }
}
